<%@page import="com.grom.service.GcmsUserService"%>
<%@page import="com.grom.util.Constants"%>
<%@page import="com.grom.po.Customer"%>
<%@page import="com.grom.service.CustomerService"%>
<%@page import="com.grom.util.SecurityUtil"%>
<%@page import="com.grom.po.GcmsUser"%>
<%@page import="com.grom.po.Brand"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/themes/icon.css">
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.6.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.easyui.min.js"></script>

<script type="text/javascript">
var currentUserId = "";
<%
	if(session.getAttribute("USER")!=null&&session.getAttribute("USER").equals("ADMIN")){
		%>
		currentUserId="ADMIN";
		<%
	}
	if(session.getAttribute("USER")!=null&&session.getAttribute(Constants.KEY_USER_SESSION)!=null){
		%>
		currentUserId="<%=((GcmsUser)session.getAttribute(Constants.KEY_USER_SESSION)).getID() %>";
		<%
	}
%>
$(function(){
	$('#loginInfoEdit').dialog({  
	    title : 'Brand 录入',  
	    modal: true,  
	    collapsible : true,    
	    resizable : true   
	});  
	$('#loginInfoEdit').dialog('close');
	$('#win').window({  
	    width:600,  
	    height:400,  
	    modal:true  
	});  
	$('#win').window('close');  // close window
});


function showAddDialog(){
	clearTheContactInfoAndHistory();
	$('input').each(function(){
		$(this).val('');
	});
	$('select').each(function(){
		$(this).val('');
	});
	$('#save').linkbutton('enable');
	$('#save1').linkbutton('enable');
	$('#save2').linkbutton('enable');
	$('#loginInfoEdit').dialog('open');
}

function loginInfoEdit(){
	var canSub = true;
	$('input').each(function(){
		if($(this).attr("required")=="true"){
			if($(this).val()==""){
				$(this).focus();
				canSub =false;
				return;
			}
		}
	});
	$('select').each(function(){
		if($(this).attr("required")=="true"){
			if($(this).val()==""){
				$(this).focus();
				canSub =false;
				return;
			}
		}
	});
	if(canSub){
		$('#brandForm').submit();
	}
}
function viewDetail(){
	var itemId =  $('#tt').datagrid('getSelected').itemid;
	$.ajax({
	    url: '<%=request.getContextPath()%>/BrandAction/getDetail.do?itemid='+itemId,
	    type: 'GET',
	    timeout: 1000,
	    error: function(){
	        alert('Error getting response!');
	    },
	    success: function(json){
	        var brand = eval(json);
	        $('#ID').val(brand.ID);
	        $('#ppId').val(brand.ppId);
	        $('#brandCode').val(brand.brandCode);
	        $('#brandName').val(brand.brandName);
	        $('#companyName').val(brand.companyName);
	        $('#webSite').val(brand.webSite);
	        $('#clothType').val(brand.clothType);
	        $('#dealScope').val(brand.dealScope);
	        $('#dealArea').val(brand.dealArea);
	        $('#storeNum').val(brand.storeNum);
	        $('#chargeScope').val(brand.chargeScope);
	        $('#averageSale').val(brand.averageSale);
	        $('#storeAcreage').val(brand.storeAcreage);
	        $('#bestSale').val(brand.bestSale);
	        $('#wayOfSale').val(brand.wayOfSale);
	        $('#everUseABD').val(brand.everUseABD);
	        $('#nameOfABD').val(brand.nameOfABD);
	        $('#Address').val(brand.Address);
	        $('#Phone').val(brand.Phone);
	        $('#Email').val(brand.Email);
	        $('#faxNum').val(brand.faxNum);
	        $('#mainManager').val(brand.mainManager);
	        $('#contactToMain').val(brand.contactToMain);
	        $('#contactToABD').val(brand.contactToABD);
	        $('#userId').val(brand.userId);
	        $('#customerId').val(brand.customerId);
	        $('#chargerType').val(brand.chargerType);
	        clearTheContactInfoAndHistory();
	        for(var i = 0 ; i < brand.contactInfo.length ; i++){
	        	var html="";
	        	html += brand.contactInfo[i].contactPerson + "-" + brand.contactInfo[i].contactPhone;
	        	if($('#contactInfo').val()==""){
					$('#contactInfoDiv').html("<span id='"+html+"'>"+html+"&nbsp;<a href='#' onclick=\"removeFromContact('"+html+"')\">remove</a></span>");
					$('#contactInfo').val(html);
				}else{
					$('#contactInfoDiv').append("<br><span id='"+html+"'>"+html+"&nbsp;<a href='#' onclick=\"removeFromContact('"+html+"')\">remove</a></span>");
					$('#contactInfo').val($('#contactInfo').val()+","+ html);
				}
	        }
	        for(var i=0 ; i < brand.historys.length; i++){
	        	$('#historyInfo').val($('#historyInfo').val()+"-"+brand.historys[i].detailInfo);
		        $('#hisToryInfoDiv').append("<p>"+brand.historys[i].detailInfo+"</p>");
	        }
	        if(currentUserId == "ADMIN"||brand.userId == currentUserId ||brand.userId==""){
	        	$('#save').linkbutton('enable');
	        	$('#save1').linkbutton('enable');
	        	$('#save2').linkbutton('enable');
	        }else{
	        	$('#save').linkbutton('disable');
	        	$('#save1').linkbutton('disable');
	        	$('#save2').linkbutton('disable');
	        }
			$('#loginInfoEdit').dialog('open');
	    }
	});
}

function clearTheContactInfoAndHistory(){
	$('#contactInfo').val("");
	$('#contactInfoDiv').html("");
	$('#historyInfo').val("");
	$('#hisToryInfoDiv').html("");
}

function doDelete(){
	var itemId =  $('#tt').datagrid('getSelected').itemid;
	if (itemId){  
        $.messager.confirm('Confirm','Are you sure you want to remove this user?',function(r){  
            if (r){  
				$('#deleteId').val(itemId);
				$('#deleteForm').submit();
            }  
        });  
    } 
}
function doSearch(){
	$('#searchForm').submit();
}

function addContact(){
	$.messager.prompt('联系人','请填写姓名：', function(val){
		var html = val;
		$.messager.prompt('联系电话','请填写电话：', function(val){
			html += "-"+val;
			if($('#contactInfo').val()==""){
				$('#contactInfoDiv').html("<span id='"+html+"'>"+html+"&nbsp;<a href='#' onclick=\"removeFromContact('"+html+"')\">remove</a></span>");
				$('#contactInfo').val(html);
			}else{
				$('#contactInfoDiv').append("<br><span id='"+html+"'>"+html+"&nbsp;<a href='#' onclick=\"removeFromContact('"+html+"')\">remove</a></span>");
				$('#contactInfo').val($('#contactInfo').val()+","+ html);
			}
		});
	});
}

function removeFromContact(place){
	var contactInfo = $('#contactInfo').val();
	if(place == contactInfo){
		contactInfo="";
	}else{
		contactInfo = contactInfo.replace(","+place, "");
	}
	$('#contactInfo').val(contactInfo);
	$('#'+place).html("");
}

function showHistoryWin(){
	$('#history').val("add your history here!");
	$('#win').window('open'); 
}

function addHistory(){
	if($('#historyInfo').val()==""){
		$('#historyInfo').val($('#history').val());
	}else{
		$('#historyInfo').val($('#historyInfo').val()+ "-" +$('#history').val());
	}
	$('#hisToryInfoDiv').append("<p>"+$('#history').val()+"</p>");
	$('#win').window('close'); 
}
</script>
</head>
<body>
<div id="win" iconCls="icon-save" title="增加拜访记录">  
  	请在下面填写拜访记录：<br>
   	<textarea id="history" name="history" rows="15" cols="70" ></textarea>
   	<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="addHistory()">Add</a> 
</div>  
<div style="display: none;">
	<form name="deleteForm" id="deleteForm" action="<%=request.getContextPath() %>/BrandAction/doDelete.do">
		<input type="text" name="deleteId" id="deleteId"/>
	</form>
</div>
<div style="font-size: 10px;">
	<form id="searchForm" name="searchForm" action="<%=request.getContextPath() %>/BrandAction/conditionSelect.do" method="post">
      <label>品牌类型：</label>  
      <input type="text" name="clothType_s" id="clothType_s" ></input>  
      <label>品牌名称：</label>  
      <input type="text" name="brandName_s" id="brandName_s" ></input>  
      <a href="#" class="easyui-linkbutton" iconCls="icon-search" plain="true" onclick="doSearch()">search</a>
    </form>
</div>
<table id="tt" class="easyui-datagrid"  singleSelect="true"
        title="DataGrid with Toolbar" iconCls="icon-save"  
        toolbar="#tb">  
    <thead>  
        <tr>  
            <th field="itemid" width="80" hidden="true">Item ID</th>  
            <th field="brandName" width="80" >品牌名称</th>  
            <th field="brandCode" width="80">品牌类型</th>  
            <th field="companyName" width="80" >品牌地址</th>  
            <th field="webSite" width="150">品牌网站</th>  
            <th field="clothType" width="150" align="center">收银系统型号</th>  
            <th field="sales" width="150" align="center">销售人员</th> 
        </tr>  
    </thead>  
    <tbody>     
    	<%
    		List<Brand> allBrands = (List<Brand>)request.getAttribute("allBrands");
    		for(Brand brand : allBrands){
    	%>
        <tr>                             
            <td><%=brand.getID() %></td>              
            <td><%=brand.getBrandName() %></td>              
            <td><%=brand.getClothType() %></td>              
            <td><%=brand.getAddress() %></td>              
            <td><%=brand.getWebSite() %></td>              
            <td><%=brand.getChargerType() %></td> 
            <td><%=GcmsUserService.instance.getUserByID(brand.getUserId()).getUserName() %></td>             
        </tr>  
    	<%
    		}
    	%>                         
    </tbody>
</table>  
<div id="tb">  
   <%
	if(session.getAttribute("USER")!=null){
%>
    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="showAddDialog()">Add</a>  
    <a href="#" class="easyui-linkbutton" iconCls="icon-cut" plain="true" onclick="viewDetail()">view/update</a>  
    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="doDelete()">Delete</a> 
    <a href="#" class="easyui-linkbutton" iconCls="icon-back" plain="true" onclick="javascript:alert('我觉着这个翻页不实用，设置下条件吧！')">pre page</a> 
    <a href="#" class="easyui-linkbutton" iconCls="icon-back" plain="true" onclick="javascript:alert('我觉着这个翻页不实用，设置下条件吧！')">next page</a> 
<%
	}
%>
</div>  


<!-- 编辑 -->  
<div id="loginInfoEdit"  
    style="padding: 5px; width: 600px; height: 450px;">  
    <h5 id="loginInfoEdit_message" style="color: red;"></h5>  
    <div class="ToolTip_Form" id="table_loginInfoEdit" onkeydown="if(event.keyCode==13){loginInfoEdit();}">  
       <form id="brandForm" name="brandForm" action="<%=request.getContextPath() %>/BrandAction/doSave.do" method="post">
       <input type="hidden" name="ID" id="ID" value="" />
       <ul>    
            <li>  
                <label>品牌名称：</label>  
                <input type="text" class="easyui-validatebox" name="brandName" id="brandName" required="true"></input>  
            </li>
            <li>  
                <label>品牌类型：</label>  
                <input type="text" class="easyui-validatebox" name="clothType" id="clothType" ></input>  
            </li> 
            <li>  
                <label>品牌网站：</label>  
                <input type="text" class="easyui-validatebox" name="webSite" id="webSite" ></input>  
            </li>
            <li>  
                <label>品牌地址：</label>  
                <input type="text" class="easyui-validatebox" name="Address" id="Address"  required="true"></input>  
            </li> 
            <li>  
                <label>品牌联系人/品牌联系人增加：</label>  
                <a id="save1" name="save1" href="#" class="easyui-linkbutton" onclick="addContact()">增加</a>
                <input type="hidden" name="contactInfo" id="contactInfo"/>
                <div id="contactInfoDiv">
                </div>
            </li> 
            <li>  
                <label>店铺数量：</label>  
                <input type="text" class="easyui-validatebox" name="storeNum" id="storeNum" ></input>  
            </li> 
            <li>  
                <label>收银系统型号：</label>  
                <input type="text" class="easyui-validatebox" name="chargerType" id="chargerType" ></input>  
            </li> 
            <li>  
                <label>拜访记录：</label>  
                <a id="save2" name="save2" href="#" class="easyui-linkbutton" onclick="showHistoryWin()">增加</a>
                <input type="hidden" name="historyInfo" id="historyInfo"/>
                <div id="hisToryInfoDiv">
                </div>
            </li> 
            <li>  
                <label>销售人员：</label>  
                <select id="userId" name="userId" required="true">
                	<option value="">无</option>
                	<%
                		List<GcmsUser> users = SecurityUtil.getAllUser();
                		for(GcmsUser user : users){
                			%>
                			<option value="<%=user.getID() %>"><%=user.getUserName() %></option>
                			<%
                		}
                	%>
                </select>
            </li> 
            <li>  
                <label>所属公司：</label>  
                <select id="customerId" name="customerId" required="true">
                	<option value="">无</option>
                	<%
                		List<Customer> customers = CustomerService.instance.getAllCustomer();
                		for(Customer cus : customers){
                			%>
                			<option value="<%=cus.getID() %>"><%=cus.getCustomerName() %></option>
                			<%
                		}
                	%>
                </select>
            </li> 
            
            
            <!-- 
            <li>  
                <label>pp_id：</label>  
                <input type="text" class="easyui-validatebox" name="ppId" id="ppId" maxlength_d="20" required_d="true"></input>  
            </li>  
            <li>  
                <label>ABD品牌代码：</label>  
                <input type="text" class="easyui-validatebox" name="brandCode" id="brandCode" maxlength_d="20" required="true"></input>  
            </li> 
            <li>  
                <label>公司名称：</label>  
                <input type="text" class="easyui-validatebox" name="companyName" id="companyName" maxlength_d="20" required_d="true"></input>  
            </li> 
            <li>  
                <label>经营范围：</label>  
                <input type="text" class="easyui-validatebox" name="dealScope" id="dealScope" maxlength_d="20" required_d="true"></input>  
            </li> 
            <li>  
                <label>经营地域（省、市）：</label>  
                <input type="text" class="easyui-validatebox" name="dealArea" id="dealArea" maxlength_d="20" required_d="true"></input>  
            </li> 
            <li>  
                <label>价格区间(start~end)：</label>  
                <input type="text" class="easyui-validatebox" name="chargeScope" id="chargeScope" maxlength_d="20" required_d="true"></input>  
            </li> 
            <li>  
                <label>店铺每月平均销售额(元)：</label>  
                <input type="text" class="easyui-validatebox" name="averageSale" id="averageSale" maxlength_d="20" required_d="true"></input>  
            </li> 
            <li>  
                <label>平均店面大小（m2）：</label>  
                <input type="text" class="easyui-validatebox" name="storeAcreage" id="storeAcreage" maxlength_d="20" required_d="true"></input>  
            </li> 
            <li>  
                <label>品牌最高年销售额：</label>  
                <input type="text" class="easyui-validatebox" name="bestSale" id="bestSale" maxlength_d="20" required_d="true"></input>  
            </li> 
            <li>  
                <label>品牌基本推销方式：</label>  
                <input type="text" class="easyui-validatebox" name="wayOfSale" id="wayOfSale" maxlength_d="20" required_d="true"></input>  
            </li> 
            <li>  
                <label>有无店铺已安装ABD：</label>  
                <input type="text" class="easyui-validatebox" name="everUseABD" id="everUseABD" maxlength_d="20" required_d="true"></input>  
            </li> 
            <li>  
                <label>已安装ABD系统的名称：</label>  
                <input type="text" class="easyui-validatebox" name="nameOfABD" id="nameOfABD" maxlength_d="20" required_d="true"></input>  
            </li> 
            <li>  
                <label>联系电话：</label>  
                <input type="text" class="easyui-validatebox" name="Phone" id="Phone" maxlength_d="20" required_d="true"></input>  
            </li> 
            <li>  
                <label>联系电邮：</label>  
                <input type="text" class="easyui-validatebox" name="Email" id="Email" maxlength_d="20" required_d="true"></input>  
            </li> 
            <li>  
                <label>联系传真：</label>  
                <input type="text" class="easyui-validatebox" name="faxNum" id="faxNum" maxlength_d="20" required_d="true"></input>  
            </li> 
            <li>  
                <label>品牌主要负责人：</label>  
                <input type="text" class="easyui-validatebox" name="mainManager" id="mainManager" maxlength_d="20" required_d="true"></input>  
            </li> 
            <li>  
                <label>品牌主要负责人联系方式：</label>  
                <input type="text" class="easyui-validatebox" name="contactToMain" id="contactToMain" maxlength_d="20" required_d="true"></input>  
            </li> 
            <li>  
                <label>ABD联系人：</label>  
                <input type="text" class="easyui-validatebox" name="contactToABD" id="contactToABD" maxlength_d="20" required_d="true"></input>  
            </li> 
			-->
			 
            <li>  
             	<a id="save" name="save" href="#" class="easyui-linkbutton" onclick="loginInfoEdit();">保存</a>
            	<a href="#" class="easyui-linkbutton" onclick="$('#loginInfoEdit').dialog('close');">放弃操作</a>
            </li>  
        </ul>  
       </form>
    </div>  
</div>  
</body>
</html>